import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'dva';
import { DATA_ENTRY, AdvancedSearch } from '../../../../../../comps';
import moment from 'moment';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message } from 'antd';
import config from './config';
import booleanList from '../../../../../../utils/enumeratedValues/trueOrFalse';

@Form.create()
class InitFormC extends PureComponent {
  state = {
    optimizationGoals: '1', // 优化目标默认值
    disabledForm: false, // 等权重的时候 为true置灰
  };

  componentDidMount() {
    this.props.onRef(this);
  }

  // 优化模型切换
  optimizeObjectChange = (e) => {
    // value: "1", title: 'Barra' },{ value: "2", title: '均值-方差' 3-等权重
    if (e == 3) {
      // Barra因子约束当选择“等权重”模型时，除了时间区间其余全部置灰
      this.state.searchRef?.props.form.setFieldsValue({
        assetConst: undefined,
        optimizeObject: undefined,
        barraConst: undefined,
        optimizationGoals: undefined,
        expectReturn: undefined,
        riskAversion: undefined,
      });
      this.props.handleSelectChange('0', 'barraConst');
      this.props.handleSelectChange('0', 'expectReturn');
      this.setState({ disabledForm: true });
      return;
    }
    this.setState({ disabledForm: false });
    // Barra因子约束当选择“Barra”模型时，默认“是”；
    // 当选择“均值-方差”模型时，默认“否”；
    // 当选择“是”时，下方出现【风险因子约束设置】
    this.state.searchRef?.props.form.setFieldsValue({
      barraConst: e == 1 ? '1' : '0',
    });
    this.props.handleSelectChange(e == 1 ? '1' : '0', 'barraConst');
  };

  // 优化目标
  optimizationGoalsChange = (e) => {
    // 当优化目标为“最大化效用”时展示； 风险厌恶系数
    this.setState({
      optimizationGoals: e,
    });
  };

  // 重置表单
  reset = () => {
    this.state.searchRef?.props.form.resetFields();
    this.state.HOlDDATERANGEC?.selOnChange('4');
  };

  // 回填详情
  settingDetails = (data) => {
    if (!data) return;
    this.setState({
      optimizationGoals: data.optimizationGoals,
    });
    this.state.searchRef?.props.form.setFieldsValue({
      assetConst: data.assetConst !== null ? `${data.assetConst}` : undefined,
      optimizeObject: data.optimizeObject !== null ? `${data.optimizeObject}` : undefined,
      barraConst: data.barraConst !== null ? `${data.barraConst}` : undefined,
      optimizationGoals: data.optimizationGoals !== null ? `${data.optimizationGoals}` : undefined,
      expectReturn: data.expectReturn !== null ? `${data.expectReturn}` : undefined,
      riskAversion: data.riskAversion !== null ? `${data.riskAversion}` : undefined,
    });
    setTimeout(() => {
      // 回填时间区间
      this.state.HOlDDATERANGEC?.state.COMPLEXDATE.setSelctAndDate('0', moment(data.beginDate), moment(data.endDate));
    }, 300);
    this.props.handleSelectChange(data.barraConst, 'barraConst');
    // eslint-disable-next-line eqeqeq
    if (data.optimizeObject == '3') {
      this.setState({
        disabledForm: true,
      });
    }
  };

  // 提交数据
  submit = () => {
    let data;
    this.state.searchRef?.props.form.validateFields((err, values) => {
      if (err) return;
      if (!(values.beginDate && values.endDate)) {
        message.warning('时间区间必选！');
      }
      data = values;
    });
    return data;
  };

  // 结束时间变更
  endOnChange = (e) => {
    const { typeStatus } = this.props;
    if (typeStatus == '3' || typeStatus == '2') {
      // 查看详情时候，不请求
      return;
    }
    console.log('结束时间变更 更新持仓设置列表');
    // 结束时间变更且开始时间有值 才进行请求
    this.props.getHoldList(e); // 通知父组件
  };

  render() {
    const { typeStatus, productCode, portfolioType } = this.props;
    const { disabledForm } = this.state;
    const searchProps = {
      dataSource: [
        {
          selectIndex: 'timeRange',
          startIndex: 'beginDate',
          endIndex: 'endDate',
          title: '时间区间',
          type: DATA_ENTRY.HOlDDATERANGEC,
          productCode,
          barraArgs: {
            portfolioType,
            flag: 1, // 表示持仓日期 还是资金+持仓日期
          },
          disabledEndForm: portfolioType == 1, // 真实组合结束时间不可变更
          endOnChange: this.endOnChange,
          onRef: (ref) => this.setState({ HOlDDATERANGEC: ref }),
          disabledProps: typeStatus == '3',
          initialDisabled: typeStatus == '2', // 编辑回填详情时候 初始化不请求数据
        },
        {
          dataIndex: 'optimizeObject',
          title: '优化模型',
          type: DATA_ENTRY.SELECT,
          initialValue: '1',
          optionTitle: true,
          rules: [{ required: true, message: '请选择优化模型！' }],
          onChange: this.optimizeObjectChange,
          dataSource: config.optimizeObject,
          disabled: typeStatus == '3',
        },
        {
          dataIndex: 'optimizationGoals',
          title: '优化目标',
          type: DATA_ENTRY.SELECT,
          dataSource: config.optimizationGoals,
          initialValue: '1',
          onChange: this.optimizationGoalsChange,
          optionTitle: true,
          rules: [{ required: disabledForm == false, message: '请选择优化目标！' }],
          disabled: typeStatus == '3' || disabledForm == true,
        },
        {
          title: '风险厌恶系数', // 显示名称
          dataIndex: 'riskAversion', // Form识别的Item ID
          initialValue: 5,
          type: DATA_ENTRY.INPUTNUMBER,
          min: 0,
          max: 10,
          step: 1,
          helper: '请输入0-10之间的数字',
          rules: [{ required: disabledForm == false, message: '请输入风险厌恶系数！' }],
          disabled: typeStatus == '3',
          hide: this.state.optimizationGoals != '1' || disabledForm == true,
        },
        {
          title: '预期收益率', // 显示名称
          dataIndex: 'expectReturn', // Form识别的Item ID
          dataSource: booleanList,
          type: DATA_ENTRY.SELECT,
          initialValue: '0',
          onChange: (e) => this.props.handleSelectChange(e, 'expectReturn'),
          optionTitle: true,
          helper: '使用对资产的预期收益率作为优化参数',
          rules: [{ required: disabledForm == false, message: '请选择预期收益率！' }],
          disabled: typeStatus == '3' || disabledForm == true,
        },
        {
          title: 'Barra因子约束', // 显示名称
          dataIndex: 'barraConst', // Form识别的Item ID
          dataSource: booleanList,
          type: DATA_ENTRY.SELECT,
          initialValue: '1',
          onChange: (e) => this.props.handleSelectChange(e, 'barraConst'),
          optionTitle: true,
          rules: [{ required: disabledForm == false, message: '请选择Barra因子约束！' }],
          disabled: typeStatus == '3' || disabledForm == true,
        },
        {
          title: '权重约束', // 显示名称
          dataIndex: 'assetConst', // Form识别的Item ID
          type: DATA_ENTRY.SELECT,
          dataSource: booleanList,
          initialValue: '0',
          onChange: (e) => this.props.handleSelectChange(e, 'assetConst'),
          optionTitle: true,
          rules: [{ required: disabledForm == false, message: '请选择权重约束！' }],
          disabled: typeStatus == '3' || disabledForm == true,
        },
      ],
      onRef: (ref) => this.setState({ searchRef: ref }),
      allowQuery: false,
      allowClear: false,
    };
    return (
      <div style={{ marginBottom: '15px' }}>
        <AdvancedSearch {...searchProps} />
      </div>
    );
  }
}

// props类型
InitFormC.propsTypes = {
  currentModel: PropTypes.number, // 优化模型，
  selectCurrent: PropTypes.func, // 切换列表是否下拉表格变化
  handleSelectChange: PropTypes.func, // 优化模型切换
};
export default connect(({ globalM, portfolioOptimizationM }) => ({ globalM, portfolioOptimizationM }))(InitFormC);
